package controller.expertInfo.illegalBehavior;

import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import controller.common.StaticConstant;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import response.CommonResult;
import services.dictionary.ItemTypeServices;
import services.expertInfo.ExpertBaseServices;
import services.expertInfo.IllegalBehaviorServices;
import util.JsonUtil;
import validator.NotBlankFluentValidator;
import validator.NotNullFluentValidator;
import wt.entity.expertInfo.IllegalBehavior;
import wt.entity.expertInfo.IllegalBehaviorExample;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/12/2.
 */
@Controller

@RequestMapping("expertInfo/illegalBehavior")
public class IllegalBehaviorController {
    @Autowired
    IllegalBehaviorServices illegalBehaviorServices;
    @Autowired
    ExpertBaseServices expertBaseServices;
    @Autowired
    ItemTypeServices itemTypeServices;


    @RequiresPermissions("expertInfo:illegalBehavior:default")
    @RequestMapping("index")
    public String index(Map map) throws Exception {

        map.put("itemTypeAll",  JsonUtil.obj2Json(itemTypeServices.selectByExample(null)));
        return "expertInfo/illegalBehavior/index";

    }


    @RequiresPermissions("expertInfo:illegalBehavior:default")
    @RequestMapping("select")
    @ResponseBody
    public List<IllegalBehavior> select() throws Exception {
        IllegalBehaviorExample illegalBehaviorExample=new IllegalBehaviorExample();
        illegalBehaviorExample.setOrderByClause("id desc");
        List<IllegalBehavior> illegalBehaviorList = illegalBehaviorServices.selectByExample(illegalBehaviorExample);
        for (IllegalBehavior illegalBehavior : illegalBehaviorList) {
            illegalBehavior.setExpertBase(expertBaseServices.selectByPrimaryKey(illegalBehavior.getExpertId()));
        }
        return illegalBehaviorList;
    }


    @RequiresPermissions("expertInfo:illegalBehavior:default")
    @RequestMapping("editDlg")
    public String editDlg(Map map) throws Exception {
        map.put(StaticConstant.EDIT_URL, "expertInfo/illegalBehavior/insert");
        map.put(StaticConstant.EDIT_OPERATE_TYPE, StaticConstant.EDIT_INSERT);
        return "expertInfo/illegalBehavior/editDlg";
    }

    @RequiresPermissions("expertInfo:illegalBehavior:default")
    @ResponseBody
    @RequestMapping("insert")
    public CommonResult insert(IllegalBehavior illegalBehavior) throws Exception {
        CommonResult commonResult = CommonResult.newCommonResult();
        ComplexResult result = FluentValidator.checkAll().failOver()
                .on(illegalBehavior.getCreateTime(), new NotNullFluentValidator("发生时间"))
                .on(illegalBehavior.getReason(), new NotBlankFluentValidator("原因"))
                .on(illegalBehavior.getItemId(), new NotBlankFluentValidator("事项"))
                .on(illegalBehavior.getExpertId(), new NotBlankFluentValidator("专家"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            commonResult.addMessage(result);
            return commonResult.setError();
        }
        commonResult.setStatus(illegalBehaviorServices.insertSelective(illegalBehavior));
        return commonResult;
    }


    @RequiresPermissions("expertInfo:illegalBehavior:default")
    @ResponseBody
    @RequestMapping("delete")
    public CommonResult delete(String id) throws Exception {
        CommonResult commonResult = CommonResult.newCommonResult();
        commonResult.setStatus(illegalBehaviorServices.deleteByPrimaryKey(id));
        return commonResult;
    }


}
